home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / arp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-21  |  4.1 KB  |  166 lines

  1. /*
  2.   arp.c
  3.  
  4.   ARP cache routines.
  5.   
  6.   Copyright (c) 1999 Dug Song <dugsong@monkey.org>
  7.   All rights reserved, all wrongs reversed.
  8.  
  9.   Redistribution and use in source and binary forms, with or without
  10.   modification, are permitted provided that the following conditions
  11.   are met:
  12.  
  13.   1. Redistributions of source code must retain the above copyright
  14.      notice, this list of conditions and the following disclaimer.
  15.   2. Redistributions in binary form must reproduce the above copyright
  16.      notice, this list of conditions and the following disclaimer in the
  17.      documentation and/or other materials provided with the distribution.
  18.   3. The name of author may not be used to endorse or promote products
  19.      derived from this software without specific prior written permission.
  20.  
  21.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  22.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  24.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  
  32.   $Id: arp.c,v 1.3 2000/01/21 08:20:26 dugsong Exp $
  33. */
  34.  
  35. #include <sys/types.h>
  36. #include <sys/param.h>
  37. #include <sys/socket.h>
  38. #ifdef BSD
  39. #include <sys/sysctl.h>
  40. #include <net/if_dl.h>
  41. #include <net/route.h>
  42. #ifdef __FreeBSD__    /* XXX */
  43. #define ether_addr_octet octet
  44. #endif
  45. #else /* !BSD */
  46. #include <sys/ioctl.h>
  47. #ifndef __linux__
  48. #include <sys/sockio.h>
  49. #endif
  50. #endif /* !BSD */
  51. #include <net/if.h>
  52. #include <netinet/in_systm.h>
  53. #include <netinet/in.h>
  54. #include <netinet/if_ether.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <unistd.h>
  58.  
  59. #ifdef BSD
  60.  
  61. int
  62. arp_cache_lookup(u_long ip, struct ether_addr *ether)
  63. {
  64.   int mib[6];
  65.   size_t len;
  66.   char *buf, *next, *end;
  67.   struct rt_msghdr *rtm;
  68.   struct sockaddr_inarp *sin;
  69.   struct sockaddr_dl *sdl;
  70.   
  71.   mib[0] = CTL_NET;
  72.   mib[1] = AF_ROUTE;
  73.   mib[2] = 0;
  74.   mib[3] = AF_INET;
  75.   mib[4] = NET_RT_FLAGS;
  76.   mib[5] = RTF_LLINFO;
  77.   
  78.   if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
  79.     return (-1);
  80.  
  81.   if ((buf = (char *)malloc(len)) == NULL)
  82.     return (-1);
  83.  
  84.   if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  85.     free(buf);
  86.     return (-1);
  87.   }
  88.   end = buf + len;
  89.   
  90.   for (next = buf ; next < end ; next += rtm->rtm_msglen) {
  91.     rtm = (struct rt_msghdr *)next;
  92.     sin = (struct sockaddr_inarp *)(rtm + 1);
  93.     sdl = (struct sockaddr_dl *)(sin + 1);
  94.     
  95.     if (sin->sin_addr.s_addr == ip && sdl->sdl_alen) {
  96.       memcpy(ether->ether_addr_octet, LLADDR(sdl), ETHER_ADDR_LEN);
  97.       free(buf);
  98.       return (0);
  99.     }
  100.   }
  101.   free(buf);
  102.  
  103.   return (-1);
  104. }
  105.  
  106. #else /* !BSD */
  107.  
  108. #ifndef ETHER_ADDR_LEN    /* XXX - Solaris */
  109. #define ETHER_ADDR_LEN    6
  110. #endif
  111.  
  112. int
  113. arp_cache_lookup(u_long ip, struct ether_addr *ether)
  114. {
  115.   int sock;
  116.   struct arpreq ar;
  117.   struct sockaddr_in *sin;
  118.  
  119.   memset((char *)&ar, 0, sizeof(ar));
  120. #ifdef __linux__
  121.   strncpy(ar.arp_dev, "eth0", sizeof(ar.arp_dev));   /* XXX - *sigh* */
  122. #endif
  123.   sin = (struct sockaddr_in *)&ar.arp_pa;
  124.   sin->sin_family = AF_INET;
  125.   sin->sin_addr.s_addr = ip;
  126.   
  127.   if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  128.     return (-1);
  129.   }
  130.   if (ioctl(sock, SIOCGARP, (caddr_t)&ar) == -1) {
  131.     close(sock);
  132.     return (-1);
  133.   }
  134.   close(sock);
  135.   memcpy(ether->ether_addr_octet, ar.arp_ha.sa_data, ETHER_ADDR_LEN);
  136.   
  137.   return (0);
  138. }
  139.  
  140. #endif /* !BSD */
  141.  
  142. #ifdef STANDALONE
  143. int
  144. main(int argc, char *argv[])
  145. {
  146.   struct ether_addr ether;
  147.   u_long ip;
  148.   
  149.   if (argc != 2) {
  150.     fprintf(stderr, "Usage: %s ip\n", argv[0]);
  151.     exit(1);
  152.   }
  153.   ip = inet_addr(argv[1]);
  154.   
  155.   if (arp_cache_lookup(ip, ðer) == -1) {
  156.     perror("arp_table_lookup");
  157.     exit(1);
  158.   }
  159.   printf("(%s) at %s\n", argv[1], ether_ntoa(ðer));
  160.   
  161.   exit(0);
  162. }
  163. #endif /* STANDALONE */
  164.  
  165. /* 5000 */
  166.